package user.forms;

import helpers.LanguageHelper;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

public class CaptchaForm {
	
	private static String privateKey = "6LeA0c4SAAAAAIadFi6R4xiBkjLDo4jImT2S35LZ";
	
	public static TreeMap checkValid(HttpServletRequest request, TreeMap check) {
		String remoteAddr = request.getRemoteAddr();
		ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
		reCaptcha.setPrivateKey(privateKey);
		
		LanguageHelper translate= (LanguageHelper) request.getAttribute("translate");
		
		String challenge = request.getParameter("recaptcha_challenge_field");
		String uresponse = request.getParameter("recaptcha_response_field");
		if(challenge == null)
		{
		        challenge = "b";
		}
		if(uresponse == null)
		{
		        uresponse = "a";
		}
		
		ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
		
		if (!reCaptchaResponse.isValid()) {
			check.put("captcha", translate.getString("captcha_error_input"));
		}
		return check;
	}
}
